using System;
using System.Text;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using RealState.BusinessObjects;

namespace RealState.DAL
{
	public class CategoriesDAO
	{
		public CategoriesDAO()
		{
			DbProviderHelper.GetConnection();
		}
		public List<Categories> GetCategoriess()
		{
			try
			{
				List<Categories> lstCategoriess = new List<Categories>();
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("SELECTCategoriess",CommandType.StoredProcedure);
				DbDataReader oDbDataReader = DbProviderHelper.ExecuteReader(oDbCommand);
				while (oDbDataReader.Read())
				{
					Categories oCategories = new Categories();
                    oCategories.RowNum = Convert.ToInt32(oDbDataReader["RowNumber"]);
					oCategories.Id = Convert.ToInt32(oDbDataReader["Id"]);
					oCategories.Title = Convert.ToString(oDbDataReader["Title"]);
					oCategories.ParentId = Convert.ToInt32(oDbDataReader["ParentId"]);
					oCategories.IsActive = Convert.ToBoolean(oDbDataReader["IsActive"]);
					oCategories.IsRemove = Convert.ToBoolean(oDbDataReader["IsRemove"]);
					oCategories.CategoryType = Convert.ToInt32(oDbDataReader["CategoryType"]);
					oCategories.Position = Convert.ToInt32(oDbDataReader["Position"]);
                    oCategories.Deep = Convert.ToInt32(oDbDataReader["Deep"]);
                    oCategories.Code = Convert.ToString(oDbDataReader["Code"]);
					lstCategoriess.Add(oCategories);
				}
				oDbDataReader.Close();
				return lstCategoriess;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

        public List<Categories> GetCategoriesType(int type)
        {
            List<Categories> lstCategoriess = new List<Categories>();
            try
            {
             
                DbCommand oDbCommand = DbProviderHelper.CreateCommand("SELECTCategoriesType", CommandType.StoredProcedure);
                oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CategoryType", DbType.Int32, type));
                DbDataReader oDbDataReader = DbProviderHelper.ExecuteReader(oDbCommand);
                while (oDbDataReader.Read())
                {
                    Categories oCategories = new Categories();
                    oCategories.Id = Convert.ToInt32(oDbDataReader["Id"]);
                    oCategories.Title = Convert.ToString(oDbDataReader["Title"]);
                    oCategories.ParentId = Convert.ToInt32(oDbDataReader["ParentId"]);
                    oCategories.IsActive = Convert.ToBoolean(oDbDataReader["IsActive"]);
                    oCategories.IsRemove = Convert.ToBoolean(oDbDataReader["IsRemove"]);
                    oCategories.CategoryType = Convert.ToInt32(oDbDataReader["CategoryType"]);
                    oCategories.Position = Convert.ToInt32(oDbDataReader["Position"]);
                    oCategories.Deep = Convert.ToInt32(oDbDataReader["Deep"]);
                    oCategories.Code = Convert.ToString(oDbDataReader["Code"]);
                    lstCategoriess.Add(oCategories);
                }
                oDbDataReader.Close();
                return lstCategoriess;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

		public Categories GetCategories(int Id)
		{
			try
			{
				Categories oCategories = new Categories();
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("SELECTCategories",CommandType.StoredProcedure);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Id",DbType.Int32,Id));
				DbDataReader oDbDataReader = DbProviderHelper.ExecuteReader(oDbCommand);
				while (oDbDataReader.Read())
				{
					oCategories.Id = Convert.ToInt32(oDbDataReader["Id"]);
					oCategories.Title = Convert.ToString(oDbDataReader["Title"]);
					oCategories.ParentId = Convert.ToInt32(oDbDataReader["ParentId"]);
					oCategories.IsActive = Convert.ToBoolean(oDbDataReader["IsActive"]);
					oCategories.IsRemove = Convert.ToBoolean(oDbDataReader["IsRemove"]);
					oCategories.CategoryType = Convert.ToInt32(oDbDataReader["CategoryType"]);
					oCategories.Position = Convert.ToInt32(oDbDataReader["Position"]);
                    oCategories.Deep = Convert.ToInt32(oDbDataReader["Deep"]);
                    oCategories.Code = Convert.ToString(oDbDataReader["Code"]);
				}
				oDbDataReader.Close();
				return oCategories;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int AddCategories(Categories oCategories)
		{
			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("INSERTCategories",CommandType.StoredProcedure);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Title",DbType.String,oCategories.Title));
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ParentId",DbType.Int32,oCategories.ParentId));
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@IsActive",DbType.Boolean,oCategories.IsActive));
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@IsRemove",DbType.Boolean,oCategories.IsRemove));
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CategoryType",DbType.Int32,oCategories.CategoryType));
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Position",DbType.Int32,oCategories.Position));
                oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Deep", DbType.Int32, oCategories.Deep));
                oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Code", DbType.String, oCategories.Code));

				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int UpdateCategories(Categories oCategories)
		{

			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("UPDATECategories",CommandType.StoredProcedure);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Title",DbType.String,oCategories.Title));
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ParentId",DbType.Int32,oCategories.ParentId));
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@IsActive",DbType.Boolean,oCategories.IsActive));
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@IsRemove",DbType.Boolean,oCategories.IsRemove));
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CategoryType",DbType.Int32,oCategories.CategoryType));
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Position",DbType.Int32,oCategories.Position));
                oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Deep", DbType.Int32, oCategories.Deep));
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Id",DbType.Int32,oCategories.Id));
                oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Code", DbType.String, oCategories.Code));
				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int RemoveCategories(Categories oCategories)
		{

			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("DELETECategories",CommandType.StoredProcedure);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Id",DbType.Int32,oCategories.Id));
				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int RemoveCategories(int Id)
		{

			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("DELETECategories",CommandType.StoredProcedure);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Id",DbType.Int32,Id));
				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
	}
}
